﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows;
using System.Windows.Media;

namespace Xceed.Silverlight.Utils
{
  internal static class LayoutUtils
  {
    #region Static Fields

    public static Point Origin = new Point( 0.0d, 0.0d );

    private const double Epsilon = 0.01d;
    private static Point EmptyPoint = new Point();

    #endregion

    public static void SetClipToBounds( UIElement element, Size constraint )
    {
      Rect clipBounds = new Rect( LayoutUtils.EmptyPoint, constraint );

      RectangleGeometry currentClip = element.Clip as RectangleGeometry;
      if( currentClip == null )
      {
        element.Clip = new RectangleGeometry() { Rect = clipBounds };
      }
      else
      {
        currentClip.Rect = clipBounds;
      }
    }

    public static bool AreClose( double value1, double value2 )
    {
      return ( value1 == value2 )
          || ( LayoutUtils.Distance( value1, value2 ) <= LayoutUtils.Epsilon );
    }

    public static bool AreClose( Point value1, Point value2 )
    {
      return ( value1 == value2 )
          || ( ( LayoutUtils.AreClose( value1.X, value2.X ) )
            && ( LayoutUtils.AreClose( value1.Y, value2.Y ) ) );
    }

    public static bool AreClose( Size value1, Size value2 )
    {
      return ( value1 == value2 )
          || ( ( LayoutUtils.AreClose( value1.Width, value2.Width ) )
            && ( LayoutUtils.AreClose( value1.Height, value2.Height ) ) );
    }

    private static double Distance( double value1, double value2 )
    {
      return Math.Abs( value1 - value2 );
    }
  }
}
